// pages/letters/letters.js
const lettersData = [
  {"letter": "A", "lowercase": "a", "name": "A", "sound": "/sounds/letter-a.mp3"},
  {"letter": "B", "lowercase": "b", "name": "B", "sound": "/sounds/letter-b.mp3"},
  {"letter": "C", "lowercase": "c", "name": "C", "sound": "/sounds/letter-c.mp3"},
  {"letter": "D", "lowercase": "d", "name": "D", "sound": "/sounds/letter-d.mp3"},
  {"letter": "E", "lowercase": "e", "name": "E", "sound": "/sounds/letter-e.mp3"},
  {"letter": "F", "lowercase": "f", "name": "F", "sound": "/sounds/letter-f.mp3"},
  {"letter": "G", "lowercase": "g", "name": "G", "sound": "/sounds/letter-g.mp3"},
  {"letter": "H", "lowercase": "h", "name": "H", "sound": "/sounds/letter-h.mp3"},
  {"letter": "I", "lowercase": "i", "name": "I", "sound": "/sounds/letter-i.mp3"},
  {"letter": "J", "lowercase": "j", "name": "J", "sound": "/sounds/letter-j.mp3"},
  {"letter": "K", "lowercase": "k", "name": "K", "sound": "/sounds/letter-k.mp3"},
  {"letter": "L", "lowercase": "l", "name": "L", "sound": "/sounds/letter-l.mp3"},
  {"letter": "M", "lowercase": "m", "name": "M", "sound": "/sounds/letter-m.mp3"},
  {"letter": "N", "lowercase": "n", "name": "N", "sound": "/sounds/letter-n.mp3"},
  {"letter": "O", "lowercase": "o", "name": "O", "sound": "/sounds/letter-o.mp3"},
  {"letter": "P", "lowercase": "p", "name": "P", "sound": "/sounds/letter-p.mp3"},
  {"letter": "Q", "lowercase": "q", "name": "Q", "sound": "/sounds/letter-q.mp3"},
  {"letter": "R", "lowercase": "r", "name": "R", "sound": "/sounds/letter-r.mp3"},
  {"letter": "S", "lowercase": "s", "name": "S", "sound": "/sounds/letter-s.mp3"},
  {"letter": "T", "lowercase": "t", "name": "T", "sound": "/sounds/letter-t.mp3"},
  {"letter": "U", "lowercase": "u", "name": "U", "sound": "/sounds/letter-u.mp3"},
  {"letter": "V", "lowercase": "v", "name": "V", "sound": "/sounds/letter-v.mp3"},
  {"letter": "W", "lowercase": "w", "name": "W", "sound": "/sounds/letter-w.mp3"},
  {"letter": "X", "lowercase": "x", "name": "X", "sound": "/sounds/letter-x.mp3"},
  {"letter": "Y", "lowercase": "y", "name": "Y", "sound": "/sounds/letter-y.mp3"},
  {"letter": "Z", "lowercase": "z", "name": "Z", "sound": "/sounds/letter-z.mp3"}
];

Page({
  data: {
    letters: [],
    currentLetter: '',
    currentSound: '',
    audioContext: null
  },

  onLoad() {
    this.setData({
      letters: lettersData.map(item => ({
        ...item,
        animate: false
      }))
    });
    // 创建音频上下文
    this.audioContext = wx.createInnerAudioContext();
  },

  onUnload() {
    // 销毁音频上下文
    if (this.audioContext) {
      this.audioContext.destroy();
    }
  },

  // 播放字母发音
  playLetter(e) {
    const { letter, index } = e.currentTarget.dataset;
    const letterData = this.data.letters[index];
    
    // 重置动画状态
    const letters = this.data.letters.map((item, i) => ({
      ...item,
      animate: i === index
    }));

    this.setData({
      currentLetter: letter,
      currentSound: letterData.sound,
      letters
    });

    // 播放音频
    if (this.audioContext) {
      this.audioContext.src = letterData.sound;
      this.audioContext.play();
    }

    // 1秒后重置动画
    setTimeout(() => {
      const resetLetters = this.data.letters.map(item => ({
        ...item,
        animate: false
      }));
      this.setData({
        letters: resetLetters,
        currentLetter: ''
      });
    }, 1000);
  },

  onAudioEnd() {
    // 音频播放结束
  }
});

